package xyz.lisys.utils;

import java.util.ArrayList;
import java.util.HashMap;

public class MapBuilder<T> {
    private final HashMap<String, T> hashMap;

    private MapBuilder() {
        hashMap = new HashMap<>();
    }

    public static <T> MapArrayBuilder<T> array() {
        return new MapArrayBuilder<>();
    }

    public static <T> MapBuilder<T> $_() {
        return new MapBuilder<>();
    }

    public MapBuilder<T> $(String key, T obj) {
        hashMap.put(key, obj);
        return this;
    }

    public HashMap<String, T> _$() {
        return hashMap;
    }

    public static class MapArrayBuilder<T> {
        private final ArrayList<HashMap<String, T>> arrayList;
        private MapBuilder<T> mapBuilder;
        private boolean isInit = false;

        private MapArrayBuilder() {
            arrayList = new ArrayList<>();
            mapBuilder = new MapBuilder<>();
        }

        public MapArrayBuilder<T> $_() {
            if (isInit) {
                arrayList.add(mapBuilder._$());
                mapBuilder = new MapBuilder<>();
            } else {
                isInit = true;
            }
            return this;
        }

        public MapArrayBuilder<T> $(String key, T obj) {
            mapBuilder.$(key, obj);
            return this;
        }

        public ArrayList<HashMap<String, T>> _$() {
            $_();
            return arrayList;
        }
    }
}
